package bean;

import java.util.ArrayList;
import java.util.Calendar;

import bean.exceptions.PositionIdTakenException;

/**
 * Order bean
 * @author Martin Schophaus
 */
public class Order {
	private int id;
	private int customer_id;
	private Customer customer;
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	private ArrayList<LineItem> line_items = new ArrayList<LineItem>();
	private int status;
	private double delivery_charges = 0;
	private Calendar order_date;
	
	public int getCustomer_id() {
		return customer_id;
	}

	public void setCustomer_id(int customer_id) {
		this.customer_id = customer_id;
	}
	
	public Order(int id, int customer_id, int status, Calendar order_date, double delivery_charges){
		this(id, customer_id, status, order_date);
		this.setDelivery_charges(delivery_charges);
	}
	
	public Order(int id, int customer_id, int status, Calendar order_date){
		this(id, customer_id, status);
		this.setOrder_date(order_date);
	}
	
	public Order(int id, int customer_id, int status){
		this.setId(id);
		this.setStatus(status);
		this.setCustomer_id(customer_id);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public ArrayList<LineItem> getLine_items() {
		return line_items;
	}

	public void setLine_items(ArrayList<LineItem> line_items) {
		this.line_items = line_items;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public double getDelivery_charges() {
		return delivery_charges;
	}

	public void setDelivery_charges(double delivery_charges) {
		this.delivery_charges = delivery_charges;
	}
	
	public Calendar getOrder_date() {
		return order_date;
	}

	public void setOrder_date(Calendar order_date) {
		this.order_date = order_date;
	}

	public void addLine_item(LineItem line_item) throws PositionIdTakenException{
		for(LineItem item : this.line_items){
			if(item.getPosition_id() == line_item.getPosition_id()){
				throw new PositionIdTakenException("Position ID is aleady taken.");
			}
		}
		this.line_items.add(line_item);
	}
	
	public void removeLine_item(int position_id) {
		for(LineItem item : this.line_items){
			if(item.getPosition_id() == position_id) {
				this.line_items.remove(item);
				break;
			}
		}
	}
	
	@Override
	public String toString() {
		return "" + this.id + " " + this.customer_id + " " + this.getCustomer().getFname() + " " + this.getCustomer().getName() + " " + this.order_date.getTime().toString();
	}
}
